package com.android.djlmapp.activity.my;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.djlmapp.R;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.dialog.MyUsualyDialog;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.djlmapp.tools.global.UserInfoUtils;
import com.android.readcard.eventbus.Event;
import com.android.readcard.eventbus.EventBusUtil;
import com.android.readcard.eventbus.EventCodes;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class ChangePhoneNumActivity extends BaseActivity {
    public static final String TAG = "djlm-my: ";
    @BindView(R.id.top_bac)
    ImageView topBac;
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;
    @BindView(R.id.tv_bdsjh)
    TextView tvBdsjh;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_yzm)
    EditText edYzm;
    @BindView(R.id.tv_hqyzm)
    TextView tvHqyzm;
    @BindView(R.id.tv_change)
    TextView tvChange;
    @BindView(R.id.activity_change_phone_num)
    LinearLayout activityChangePhoneNum;

    private Boolean flag = false;
    private String mobile;
    private String yzm;
    private MyUsualyDialog dialog;
    private Unbinder mUnbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone_num);
        ButterKnife.bind(this);
        mUnbinder = ButterKnife.bind(this);

        initView();
        initData();
        dialog = new MyUsualyDialog(ChangePhoneNumActivity.this, R.layout.wait_item);
        dialog.setCanceledOnTouchOutside(false);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        tvName.setText("账户绑定");
        edYzm.setInputType(InputType.TYPE_CLASS_NUMBER);
        edPhone.setInputType(InputType.TYPE_CLASS_NUMBER);

    }

    @OnClick({R.id.ll_top_back, R.id.tv_hqyzm, R.id.tv_change, R.id.activity_change_phone_num})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_top_back:
                finish();
                break;
            case R.id.tv_hqyzm:
                    getCode();
                break;
            case R.id.tv_change:
                    changePhone();
                break;
            case R.id.activity_change_phone_num:
                break;
        }
    }

    private void getCodeNet() {
        Log.i(TAG, "edPhone: " + mobile);

        MyApplication.getInstance().getHttpTools().getCode(mobile, "0", new StringCallback() {
            @Override
            public void onSuccess(com.lzy.okgo.model.Response<String> response) {
                Log.i(TAG, "获取验证码: " + response.body());

                try{
                    tvHqyzm.setText("获取验证码");
                    tvHqyzm.setEnabled(true);

                    parseJson(response.body());
                    dialog.dismiss();
                } catch (Exception e) {
                    dialog.dismiss();
                    ToastUtils.showShortToast(ChangePhoneNumActivity.this, "数据解析异常"+e.toString());
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<String> response) {
                super.onError(response);
                tvHqyzm.setText("获取验证码");
                tvHqyzm.setEnabled(true);
                Toast.makeText(ChangePhoneNumActivity.this,
                        "请检查您的网络", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }


    private void getCode(){
        dialog.show();
        tvHqyzm.setText("获取中...");
        tvHqyzm.setEnabled(false);
        mobile = edPhone.getText().toString().trim();

        if(mobile != null && !mobile.isEmpty()){
            getCodeNet();
        }else{
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT);
        }
    }

    private void changePhone(){
        dialog.show();
        flag = true;
        mobile = edPhone.getText().toString().trim();
        yzm = edYzm.getText().toString().trim();
        tvChange.setText("绑定中");
        tvChange.setEnabled(false);

        changePhoneNet();
    }

    private void changePhoneNet() {
        MyApplication.getInstance().getHttpTools().changeNum(mobile, yzm, new StringCallback() {
            @Override
            public void onSuccess(com.lzy.okgo.model.Response<String> response) {
                Log.i(TAG, "绑定手机号: " + response.body());


                tvChange.setText("绑定手机号");
                tvChange.setEnabled(true);
                try{
                    parseJson(response.body());
                    dialog.dismiss();
                }catch(Exception e){
                    dialog.dismiss();
                    ToastUtils.showShortToast(ChangePhoneNumActivity.this, "数据解析异常"+e.toString());
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<String> response) {
                super.onError(response);
                tvChange.setText("绑定手机号");
                tvChange.setEnabled(true);
                Toast.makeText(ChangePhoneNumActivity.this,
                        "请检查您的网络", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }

    private void parseJson(String s) {
        Gson gson = new Gson();
        YzmInfo yzmInfo = gson.fromJson(s, YzmInfo.class);

        if (yzmInfo != null) {
            int count = Integer.parseInt(yzmInfo.code);
            if (count < 1000) {

                if (flag) {
                    Toast.makeText(ChangePhoneNumActivity.this,
                            "绑定成功", Toast.LENGTH_SHORT).show();

                    UserInfoUtils utils = UserInfoUtils.getUserInfoUtilsInstance();
                    utils.setMobile(mobile);

                    Event event = new Event(EventCodes.EventMyCode.B, mobile);
                    EventBusUtil.sendEvent(event);
                    // 验证成功
                    finish();
                }

            } else {
                Toast.makeText(ChangePhoneNumActivity.this,
                        "err--" + yzmInfo.msg, Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    class YzmInfo {
        public String code;
        public String msg;

    }
}
